package sort;

import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort3 {
    public static void random(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(50);
        }
    }

    //插入排序
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if (tmp < arr[j]) {
                    arr[j+1] = arr[j];
                }else {
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    // 希尔排序
    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap != 0) {
            gap /= 2;
            shell(arr,gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for(; j >= 0; j -= gap) {
                if (tmp < arr[j]) {
                    arr[j + gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            swap(arr, i, min);
        }
    }

    public static void heapSort(int[] arr) {
        createHeap(arr);

        int size = arr.length;
        while (size != 0) {
            swap(arr, 0, size - 1);
            size--;
            downward(arr, 0, size);
        }
    }

    private static void createHeap(int[] arr) {
        int size = arr.length;
        for (int parent = (size - 1 - 1) / 2; parent >= 0; parent--) {
            downward(arr, parent, size);
        }
    }

    private static void downward(int[] arr, int parent, int size) {
        int child = 2 * parent + 1;
        while (child < size) {
            if (child + 1 < size && arr[child] < arr[child+1]) {
                child = child + 1;
            }

            if (arr[child] > arr[parent]) {
                swap(arr,child,parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr, j, j+1);
                }
            }
        }
    }

    public static void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private static void quick(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }

        int pivot = arr[new Random().nextInt(r - l + 1) + l];
        int left = l - 1, i = l, right = r + 1;
        // [l, left]  [left+1, right-1]  [right, r]
        while (i < right) {
            if (arr[i] < pivot) {
                swap(arr, ++left, i++);
            }else if (arr[i] == pivot) {
                i++;
            }else {
                // arr[i] > pivot
                swap(arr, --right, i);
            }
        }

        quick(arr, l, left);
        quick(arr, right, r);
    }

//    private static int quick1(int[] arr, int left, int right) {
//        int pivot = arr[left];
//        int left = l - 1, i = l, right = r + 1;
//        // [l, left]  [left+1, right-1]  [right, r]
//        while (i < right) {
//            if (arr[i] < pivot) {
//                swap(arr, ++left, i++);
//            }else if (arr[i] == pivot) {
//                i++;
//            }else {
//                // arr[i] > pivot
//                swap(arr, --right, i);
//            }
//        }
//        return i;
//    }

    private static void quickSortNor(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0, right = arr.length - 1;
        int pivotIndex = 1;
        if (pivotIndex - 1 > left) {
            // 先压左, 再压右
            stack.push(left);
            stack.push(pivotIndex - 1);
        }
        if (pivotIndex + 1 < right) {
            stack.push(pivotIndex + 1);
            stack.push(right);
        }

        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            if (pivotIndex - 1 > left) {
                // 先压左, 再压右
                stack.push(left);
                stack.push(pivotIndex - 1);
            }
            if (pivotIndex + 1 < right) {
                stack.push(pivotIndex + 1);
                stack.push(right);
            }
        }
    }


    public static void mergeSort(int[] arr) {
        mergeFun(arr, 0, arr.length - 1);
    }

    private static void mergeFun(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }

        int mid = left + ((right - left) >> 1);
        mergeFun(arr, left , mid);
        mergeFun(arr, mid + 1, right);

        // 合并两个有序数组
        int cur1 = left, cur2 = mid + 1;
        int i = 0;
        int[] tmp = new int[right - left + 1];
        while (cur1 <= mid && cur2 <= right) {
            tmp[i++] = arr[cur1] < arr[cur2] ? arr[cur1++] : arr[cur2++];
        }

        while (cur1 <= mid) {
            tmp[i++] = arr[cur1++];
        }
        while (cur2 <= right) {
            tmp[i++] = arr[cur2++];
        }

        for (int j = 0; j < right - left + 1; j++) {
            arr[j + left] = tmp[j];
        }
    }

    public static void mergeSortNor(int[] arr) {
        int gap = 1;
        int n = arr.length;
        while(gap < n) {
            for (int k = 0; k < n; k += 2*gap) {
                int left = k;
                int mid = left + gap - 1;
                int right = mid + gap;

                if (mid >= n) {
                    mid = n - 1;
                }
                if (right >= n) {
                    right = n - 1;
                }

                int cur1 = left, cur2 = mid + 1;
                int i = 0;
                int[] tmp = new int[right - left + 1];
                while (cur1 <= mid && cur2 <= right) {
                    tmp[i++] = arr[cur1] < arr[cur2] ? arr[cur1++] : arr[cur2++];
                }

                while (cur1 <= mid) {
                    tmp[i++] = arr[cur1++];
                }
                while (cur2 <= right) {
                    tmp[i++] = arr[cur2++];
                }

                for (int j = 0; j < right - left + 1; j++) {
                    arr[j + left] = tmp[j];
                }
            }
            gap *= 2;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[10];
        random(arr);
        System.out.println("原数组:" + Arrays.toString(arr));
        mergeSortNor(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }
}
